#ifndef VOLUME_H
#define VOLUME_H

#include <QtGui>
#include <QMutex>
#include <QThread>
#include <QWaitCondition>

#include <QGLViewer/vec.h>
using namespace qglviewer;

#include "volinterface.h"

class Volume : public QThread
{
  Q_OBJECT

 public :
  Volume(QObject *parent=0);
  ~Volume();
  void setMemorySize(int);
  void setMaxLoadBlocks(int);
  void setLogger(QTextEdit*);
  void init();
  void setFilename(QList<QString>);
  void setTextureSize(int);
  void setGridSize(int, int, int);
  void setUserLimits(Vec, Vec);
  void gridSize(int&, int&, int&);
  void gridSize(int, int&, int&, int&);
  void p2gridSize(int&, int&, int&);
  void compactGridSize(int&, int&, int&);
  void blockedGridSize(int&, int&, int&);
  void p2blockedGridSize(int&, int&, int&);
  QImage histogramImage1D();
  QImage histogramImage2D();
  uchar *blockLevel();
  uchar* blockedData();
  uchar* auxData();
  int blockSize();
  bool initIdentifyValidBlocks(int);
  bool initIdentifyValidBlocks(uchar*);
  int minLod();
  int minLod(int);
  int minLod(uchar*);
  uchar *wholeVolume(int);
  uchar *loadSingleBlock(int, int, int, int);

  void setLodLevel(int);
  void setIsovalue(int);

  bool getNextBlockLot(int&, int&, int&,
		       uchar*&, uchar*&);

  void clearBlocks();
 signals :
  void readDone();

 public slots :
  void loadBlocks(QList<Vec>, int);
  void loadAllValidBlocks(int);

 protected :
  void run();

 private :
  int m_nVolumes;
  VolInterface *m_volInterface;
  QTextEdit *m_log;

  QMap<qint64, int> m_blockList;
  QList<Vec> m_compactGrid;
  QList<uchar*> m_volData;
  QList<uchar*> m_auxData;

  int m_bd, m_br, m_bc, m_nblocks;
  int m_currMaxBlocks, m_maxLoadBlocks;

  bool m_loadingBlocks;
  QMutex m_mutex;
  QWaitCondition m_condition;

  unsigned char *m_histImageData1D;
  unsigned char *m_histImageData2D;
  QImage m_histogramImage1D;
  QImage m_histogramImage2D;

  void generateHistogramImage();

};

#endif
